<?php

/**
 * This is the model class for table "firms".
 *
 * The followings are the available columns in table 'firms':
 * @property integer $id
 * @property integer $id_section
 * @property integer $id_icon
 * @property string $title
 * @property string $post
 * @property string $url_phone
 * @property string $url_map
 * @property integer $order_id
 * @property integer $enabled
 */
class Firms extends ExtActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Firms the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function _tableName()
	{
		return 'firms';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, id_section', 'required'),
			array('id_section, id_icon, order_id, enabled', 'numerical', 'integerOnly'=>true),
			array('title, url_phone, url_map', 'length', 'max'=>255),
			array('post', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('title, id_section','safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'section'=>array(
				self::BELONGS_TO,
				'Sections',
				'id_section',
			),
			'uploads'=>array(
				self::HAS_MANY,
				'Uploads',
				'id_owner',
				'condition'=>'uploads.owner_type="FIRM"',
				'order'=>'uploads.upload_date DESC',
			),			
			'icon'=>array(
				self::BELONGS_TO,
				'Uploads',
				'id_icon',
				'select' => 'uploads.file_name, uploads.alt'
			),			
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'id_section' => 'Раздел',
			'id_icon' => 'Иконка',
			'title' => 'Заголовок',
			'post' => 'Текст',
			'url_phone' => 'Ссылка на тел.книгу',
			'url_map' => 'Ссылка на карту',
			'order_id' => 'Сортировка',
			'enabled' => 'Видимый',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		$criteria=new CDbCriteria;
		
		$criteria->with=array(
			'section' => array(
				'select' => 'section.name, section.order_id'
			),
		);

		$criteria->compare('id_section',$this->id_section);
		$criteria->compare('title',$this->title,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
			'pagination'=>array('pageSize'=>25),
			'sort'=>array('defaultOrder'=>'section.order_id ASC, t.order_id ASC'),
		));
	}
}